/**
 * Created by Feil.Wang(wangfeilong@xuexibao.cn) on 2015/12/30.
 */
require(['jquery', 'common'], function ($, Common) {
    'use strict';
    var $form = $('#regForm');
    $form.submit(function () {
        var $username = $form.find('input[name="username"]');
        var $password = $form.find('input[name="password"]');
        var $repassword = $form.find('input[name="repassword"]');
        var username = $.trim($username.val());
        var password = $password.val();
        var repassword = $repassword.val();
        if (!username.length) {
            Common.tips('请输入用户名', function () {
                $username.focus();
            });
            return false;
        }
        if (!password.length) {
            Common.tips('请输入密码', function () {
                $password.focus();
            });
            return;
        }
        if (!repassword.length) {
            Common.tips('请再次输入密码', function () {
                $repassword.focus();
            });
            return false;
        }
        if (password !== repassword) {
            Common.tips('两次密码不一致', function () {
                $repassword.focus();
            });
            return false;
        }
        var data = {uname: username, upwd: password};
        $.ajax({
            url: '/register',
            type: 'post',
            dataType: 'json',
            data: data,
            success: function (res) {
                console.log(res);
                if (res.success) {
                    Common.tips(res.messages, function () {
                        location.href = '/login';
                    })
                } else {
                    Common.tips(res.messages)
                }
            }
        });
        return false;
    });
});